<?php
// +----------------------------------------------------------------------
// | zhanshop-marketing / Adupload.php    [ 2024/12/29 14:03 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2024 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\admin\v1\service\finder;

use app\provider\AdEventUploadProvider;
use zhanshop\App;
use zhanshop\Helper;
use zhanshop\server\Request;
use zhanshop\server\Response;

class Adupload extends BaseFinder
{
    protected $tabs = [
        [
            'title' => '全部应用',
            'where' => [
                'app',
                '=',
                ''
            ],
        ],
    ];

    protected $headToolbar = [];

    protected $rowToolbar = [
        [
            'title' => '重发上报',
            'event' => 'ajax',
            'templet' => '<div><i class="layui-icon">&#xe609;</i>&nbsp;&nbsp;<span class="text">{{= d.title }}</span></div>',
            'method' => 'resubmit',
            'page' => '',
        ]
    ];

    public function __construct(string $menuId)
    {
        parent::__construct($menuId);
        $apps = App::database()->model("ad_app")->order('create_time asc')->limit(20)->select();
        foreach ($apps as $app){
            $this->tabs[] = [
                'title' => $app['name'],
                'where' => [
                    'app',
                    '=',
                    $app['app']
                ],
            ];
        }
    }

    public function searchfrom(Request $request, Response $response){
        $data = parent::searchfrom($request, $response);
        $tabs = $this->tabs;
        unset($tabs[0]);
        $apps = [];
        foreach ($tabs as $tab){
            $app = $tab['where'][2];
            $apps[$app] = $tab['title'];
        }
        $data['app']['value'] = $apps;
        return $data;
    }

    /**
     * 重发上报
     * @param Request $request
     * @param Response $response
     * @return void
     */
    public function resubmit(Request $request, Response $response)
    {
        $input = $request->validateRule([
            'id' => 'required'
        ])->getData();
        $data = App::database()->model("ad_upload")->where(['id' => $input['id']])->findOrFail();
        $platform = $data['platform'];
        App::make(AdEventUploadProvider::class)->$platform($data['app'], $data['upload_event'], $data['mate_type'], json_decode($data['match_item'] ?? '', true), $data, json_decode($data['attach'] ?? '', true) , true);
    }
    /**
     * 更新
     * @param Request $request
     * @return void
     */
    public function put(Request $request, Response $response)
    {
        $input = $request->validateData($request->get(), [
            'pk' => 'Required',
        ])->getData();
        $post = $request->post();
        $uuid = $post[0]['uuid'] ?? 0;
        if($uuid){
            App::database()->model('ad_upload')->where(['id' => $input['pk']])->update(['uuid' => $uuid]);
        }
        return $this->data(1, 1, [[$this->menuData['pk'], '=', $input['pk']]]);
    }
}